package com.mapapp.flutter_mapapp.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mapapp.flutter_mapapp.R;
import com.mapapp.flutter_mapapp.bean.StepFloorBean;
import com.mapapp.flutter_mapapp.util.HorizontalSpaceItemDecoration;
import com.mapapp.flutter_mapapp.widget.BetterGesturesRecyclerView;

import java.util.ArrayList;
import java.util.List;

public class StepFloorAdapter extends RecyclerView.Adapter<StepFloorAdapter.StepFloorViewHolder>{

    private ArrayList<StepFloorBean> mFloorInfos;
    private Context mContext;
    private int mPosition = -1;

    public StepFloorAdapter(ArrayList<StepFloorBean> infos , Context content){
        this.mFloorInfos = infos;
        this.mContext = content;
    }

    public void notify(ArrayList<StepFloorBean> infos){
        this.mFloorInfos = infos;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public StepFloorViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_step_floor, parent, false);
        StepFloorViewHolder holder = new StepFloorViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull StepFloorViewHolder holder, final int position) {
        StepListAdapter adapter = new StepListAdapter(mFloorInfos.get(position),mContext);
        LinearLayoutManager llmanage = new LinearLayoutManager(mContext);
        llmanage.setOrientation(LinearLayoutManager.HORIZONTAL);
        holder.floors.setLayoutManager(llmanage);
        holder.floors.setAdapter(adapter);
        holder.linear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(onItemClickListener != null){
                    onItemClickListener.onNextClick(position);
                }
            }
        });
        adapter.setOnItemClickListener(new StepListAdapter.OnItemClickListener() {
            @Override
            public void onItemClick() {
                if(onItemClickListener != null){
                    mPosition = position;
                    notifyDataSetChanged();
                    onItemClickListener.onItemClick(position);
                }
            }
        });
        int num = 0;
        for(int i = 0 ; i < mFloorInfos.get(position).getProcesses().size() ; i ++){
            if(mFloorInfos.get(position).getProcesses().get(i).getPercent() == 1){
                num++;
            }
        }
        if(num == mFloorInfos.get(position).getProcesses().size()){
            holder.floorNum.setBackgroundResource(R.drawable.tv_floor_finish_bg);
        }else{
            holder.floorNum.setBackgroundResource(R.drawable.tv_floor_bg);
        }
        holder.floorNum.setText(mFloorInfos.get(position).getRegionName()+"层");
        if(position == mPosition){
            holder.floors.setBackgroundResource(R.color.step_item_choose);
        }else{
            holder.floors.setBackgroundResource(R.color.white);
        }
    }

    @Override
    public int getItemCount() {
        if (mFloorInfos == null) {
            return 0;
        }
        return mFloorInfos.size();
    }

    static class StepFloorViewHolder extends RecyclerView.ViewHolder {
        private BetterGesturesRecyclerView floors;
        private LinearLayout linear;
        private TextView floorNum;
        private LinearLayout llNum;

        public StepFloorViewHolder(View itemView) {
            super(itemView);
            floorNum = itemView.findViewById(R.id.tv_item_step_floor_num);
            floors = itemView.findViewById(R.id.rv_step_item);
            linear = itemView.findViewById(R.id.ll_item_step_floor_next);
            llNum = itemView.findViewById(R.id.ll_item_step_floor_num);
        }
    }

    //私有属性
    private OnItemClickListener onItemClickListener = null;

    //setter方法
    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    //回调接口
    public interface OnItemClickListener {
        void onItemClick(int position);

        void onNextClick(int position);
    }
}
